let gulp = require('gulp'),
    uglify = require('gulp-uglify'),
    rename = require('gulp-rename'),
    minify = require('gulp-minify-css'),
    connect = require('gulp-connect'),
    watch = require('gulp-watch'),
    less = require("gulp-less"),
    concat = require('gulp-concat'),
    clean = require('gulp-clean'),
    imagemin = require('gulp-imagemin'),
    autoprefixer = require('gulp-autoprefixer'), 
    pngquant = require('imagemin-pngquant');

    // 生成版本号清单
gulp.task('rev', function() {
    return gulp.src(['./dist/js/**', './dist/css/**'])
        .pipe(rev())
        .pipe(rev.manifest())
        .pipe(gulp.dest("./"));
});

gulp.task('uglifyjs', function () {
    return gulp.src('src/**/*.js')
        .pipe(uglify())
        .pipe(concat('all.js'))
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest('dist/js'))
})

gulp.task('cssminify', function() {
    return gulp.src('src/**/*.css')
        .pipe(minify())
        .pipe(rename({ suffix: '.min' }))
        .pipe(gulp.dest('dist'))
        .pipe(connect.reload())
})

gulp.task('less', function() {
    return gulp.src('src/less/*.less')
        .pipe(less())
        .pipe(minify())
        .pipe(concat('index.css'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(autoprefixer({
			overrideBrowserslist: ['last 20 versions']
		}))
        .pipe(gulp.dest('dist/css'))
        .pipe(connect.reload())
})

gulp.task('html',function() {
    return gulp.src(['src/**/*.html'])
        .pipe(gulp.dest('dist'))
        .pipe(connect.reload())
})


// 设置压缩参数
gulp.task('image', function() {
    gulp.src('src/image/**/**.*')
    // .pipe(imagemin({
    //     optimizationLevel: 5, //类型：Number  默认：3  取值范围：0-7（优化等级）
    //     progressive: true, //类型：Boolean 默认：false 无损压缩jpg图片
    //     interlaced: true, //类型：Boolean 默认：false 隔行扫描gif进行渲染
    //     multipass: true //类型：Boolean 默认：false 多次优化svg直到完全优化
    // }))
    .pipe(gulp.dest('dist/image'))
    .pipe(connect.reload())
})

// 设置
gulp.task('clean', function () {
    gulp.src('dist')
        .pipe(clean());
})

gulp.task("watch", function () {//监听任务
    watch("src/**/*.js",gulp.parallel('uglifyjs',function() {
        console.log('js压缩成功')
    }));
    watch("src/less/*.less",gulp.parallel('less',function() {
        console.log('less编译成功')
    }));
    watch("src/image/**/*.*", gulp.parallel('image',function() {
        console.log('图片压缩成功')
    }))
    watch("src/**/*.css",gulp.parallel('cssminify',function() {
        console.log('css样式压缩成功')
    }));
    watch("src/**/*.html", gulp.parallel('html',function() {
        console.log('html 拷贝成功')
    }));
    
})

gulp.task("server", function(){
	connect.server({
        root: "./dist",
		livereload: true,
		port: 8030
	})
})

gulp.task('default', gulp.parallel('watch', 'server', function () {
    console.log('操作成功')
}))
